package com.jgp.security.config;

import com.jgp.sys.repository.JGPRepositoryFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-12
 */
@Order(150)
@Configuration
@ConditionalOnProperty(prefix = "jgp.security", name = "db-enabled", havingValue = "false" ,matchIfMissing = true)
public class StanderDataSourceConfiguration {
    @Configuration
    @EnableTransactionManagement(proxyTargetClass = true)
    @EntityScan(basePackages = "com.jgp.security.secmodel",basePackageClasses = Jsr310JpaConverters.class)
    @EnableJpaRepositories(
            basePackages = "com.jgp.security.secrepository", repositoryFactoryBeanClass = JGPRepositoryFactoryBean.class,
            entityManagerFactoryRef = "secondEntityManagerFactoryBean",
            transactionManagerRef = "secondTransactionManager")
    public class AloneDataSourceConfiguration {
        
        //实体管理工厂builder
        @Resource
        private EntityManagerFactoryBuilder factoryBuilder;
        
        @Resource
        private JpaProperties jpaProperties;
        
        @Resource
        private HibernateProperties hibernateProperties;
    
        @Resource
        private DataSource dataSource;
        
        /**
         * 配置第二个实体管理工厂的bean
         * @return
         */
        @Bean(name = "secondEntityManagerFactoryBean")
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
            Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
                    jpaProperties.getProperties(), new HibernateSettings());
            return factoryBuilder.dataSource(dataSource)
                                 .properties(properties)
                                 .persistenceUnit("secondPersistenceUnit")
                                 .build();
        }
        
        /**
         * EntityManager不过解释，用过jpa的应该都了解
         * @return
         */
        @Bean(name = "secondEntityManager")
        public EntityManager entityManager() {
            return entityManagerFactoryBean().getObject().createEntityManager();
        }
        
        /**
         * jpa事务管理
         * @return
         */
        @Bean(name = "secondTransactionManager")
        public JpaTransactionManager transactionManager() {
            JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
            jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
            return jpaTransactionManager;
        }
        
        
    }
}
